package com.hy.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.common.enums.ExceptionEnums;
import com.hy.common.exception.XkException;
import com.hy.common.vo.PageResult;
import com.hy.item.mapper.BrandMapper;
import com.hy.item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandService {
    /**
     * 分页查询品牌
     * @param page
     * @param row
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @Autowired
    private BrandMapper brandMapper;
    public PageResult<Brand> queryBrandPage(Integer page, Integer row, String sortBy,
                                            Boolean desc, String key) {
        //分页，使用分页助手,第一个参数是当前页，第二个参数是每页查询的的条数
        PageHelper.startPage(page,row);//自动会在查询的sql语句中进行拦截
        //过滤
        /**
         * selece * from brand
         * where 'name' like "%x%" or letter =='x' order by id desc
         */
        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(key)){
            //添加查询条件
            Example.Criteria criteria = example.createCriteria();
                criteria.orLike("name","%"+key+"%").orEqualTo("letter",key.toUpperCase());
        }
        //排序
        if (StringUtils.isNoneBlank(sortBy)){
            //order by id desc
            String orderByClaus=sortBy+" "+(desc ? "DESC" : "ASC");
            example.setOrderByClause(orderByClaus);
        }

        List<Brand> list = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)){

            throw new XkException(ExceptionEnums.BRAND_NOT_FOUND);
        }
        //解析分页结果
        PageInfo<Brand> pageInfo = new PageInfo<>(list);//pageInfo有总条数
        return new PageResult<>(pageInfo.getTotal(),list);
    }
    /**
     * 新增品牌
     * @param brand
     * @param cids
     */
    public void saveBrand(Brand brand, List<Long> cids) {
        int count = brandMapper.insertSelective(brand);
        if (count!=1){
            //新增失败
            throw new XkException(ExceptionEnums.BRAND_SAVE_ERROR);
        }
        //新增中间表，品牌和分类表是多对多，所以需要中间表，两个字段category_id,brand_id
        //insert into tb_category_brandf
        for(Long cid:cids){
            count = brandMapper.insertCategoryBrand(cid,brand.getId());
            if (count!=1){
                //新增失败
                throw new XkException(ExceptionEnums.BRAND_SAVE_ERROR);
            }
        }
    }
    //根据spu传递过来的brand_id查询品牌名称
    public Brand queryByBrandId(Long id){

        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand==null){
            throw new XkException(ExceptionEnums.BRAND_NOT_FOUND);
        }
        return brand;

    }
    /**
     * 根据分类id查询品牌
     * @param cid
     * @return
     */
    public List<Brand> queryBrandByCid(Long cid) {
        List<Brand> list = brandMapper.queryBrandByCid(cid);
        if (CollectionUtils.isEmpty(list)){
            throw new XkException(ExceptionEnums.BRAND_NOT_FOUND);
        }
        return list;
    }
}
